#include "common_audio.h"
#include "common_audio_path.h"

/* board include*/
#include "board_includes.h"
#include "common_includes.h"

/* private define -------------------------------------------------------- */
typedef struct
{
	bool spk_status;
	bool mic_status;	//0:mute 1:unmute

	bool is_dac_active;
	struct audio_dac_channel dac_ch;
}audio_var_t;

/* private function prototypes ---------------------------------------------- */


/* private variables -------------------------------------------------------- */
static audio_var_t audio_var;

/* private function---------------------------------------------- */

/* public variables -------------------------------------------------------- */

void common_audio_init(void)
{
	memset(&audio_var, 0x0, sizeof(audio_var_t));

	ej_bsp_iis_init();

	common_audio_path_init();
}

int common_audio_dac_write(s16 *data, int len)
{
    s16 *dac_data = data;
    u16 data_len = len;
	s16 mic_dut_mono_to_dual[len * 2 + 4];
	u32 wlen = 0;
    int offset = 0;
    int cnt = 0;
#if (TCFG_AUDIO_DAC_CONNECT_MODE == DAC_OUTPUT_LR)
    for (u16 i = 0; i < (len >> 1); i++) {
        mic_dut_mono_to_dual[2 * i] 		= dac_data[i];
        mic_dut_mono_to_dual[2 * i + 1] 	= dac_data[i];
    }
    dac_data = mic_dut_mono_to_dual;
    data_len = len + data_len;
    return sound_pcm_dev_write(NULL, dac_data, data_len);
#endif
    return 0;
}

void common_audio_dac_open(int sample_rate, s16 volume)
{
	if (!audio_var.is_dac_active)
	{
		audio_var.is_dac_active = TRUE;
		sound_pcm_dev_start(NULL, sample_rate, volume);
	}
}

void common_dac_audio_close(void)
{
	if (audio_var.is_dac_active)
	{
		audio_var.is_dac_active = FALSE;
		sound_pcm_dev_stop(NULL);
	}
}

/* Audio Tools ---------------------------------------*/
#define ABS(x)                      (x > 0 ? x : (-x))
int common_audio_output_data_db_calc_simple(short *data, unsigned short len,unsigned char channels)
{
    //长度转换，如果长度是u8 需要/2
    unsigned short points = len / 2;
        unsigned short user_sample_rate = 16000;
    signed int  calc_point = user_sample_rate/50;
    static signed int  data_cnt = 0;
    static signed int  sum[4] = {0,0,0,0};
    static int average_value;

    int index = 0;

    for(index = 0; index < points; index+=channels) {
        //计算数值总和
        switch (channels) {
        case 4:
            sum[3] +=  ABS(data[index+3]);
        case 3:
            sum[2] +=  ABS(data[index+2]);
        case 2:
            sum[1] +=  ABS(data[index+1]);
        case 1:
            sum[0] +=  ABS(data[index]);
            break;
        default:
            sum[0] +=  ABS(data[index]);
            break;
        }
        data_cnt++;
        if(data_cnt>=calc_point) {
            //计算各通道取平均
            average_value= (sum[0]+sum[1]+sum[2]+sum[3])/(channels*calc_point);
            //printf("average_value:%d\n",average_value);
            sum[0] = 0;
            sum[1] = 0;
            sum[2] = 0;
            sum[3] = 0;
            data_cnt = 0;
        }
    }
    return average_value;
}
